package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.util.logging.Level;

import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.gui.components.ActionButton;
import be.kuleuven.cs.mop.gui.components.ComboSelector;
import be.kuleuven.cs.mop.gui.util.SwingUtils;



/**
 * Skeletal implementation of an {@link ActionDialog} for any "Remove ???" use case
 */
public abstract class RemoveDialog<T> extends ActionDialog {
	private static final long serialVersionUID = 1L;
	
	private ComboSelector<T> combo;
	
	
	public RemoveDialog(final Frame parent, final String title,
			final Controllers controllers) {
		super(parent, title, controllers);
		final GridBagConstraints gbc = SwingUtils.getGBC();
		
		try {
			combo = new ComboSelector<T>(getItems());
		} catch (final Exception e) {
			log(e);
			dispose();
			return;
		}
		
		gbc.gridx	= 0;
		gbc.gridy	= 0;
		add(combo, gbc);
		
		gbc.gridy	= 1;
		add(new ActionButton("Remove", new Dimension(150, 32), this), gbc);
		
		display();
	}
	
	
	
	protected abstract T[] getItems() throws Exception;
	
	@Override
	protected void handle(final ActionEvent event) throws Exception {
		final T selection = combo.getSelection();
		if (selection == null) {
			log("Please select an item", Level.WARNING);
		} else {
			remove(selection);
			log("Removed '%s'", toString(selection));
			dispose();
		}
	}
	
	protected abstract void remove(T item) throws Exception;
	
	protected abstract String toString(T item);
	
}